package com.huluobo.a2302mdmdemo.demo

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp

/**
 *  Created by LC on 2025/7/18.
 */

@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
@Composable
fun MyFlowRow() {
    //搜索记录的数据源
    val list = remember {
        //带状态的集合,当集合中的内容发生改变的时候,比如增加或者减少,使用它的列表会跟随改变,不需要刷新适配器
        SnapshotStateList<String>()
    }

    //搜索记录
    var key by remember {
        mutableStateOf("")
    }

    Column {
        OutlinedTextField(
            value = key,
            onValueChange = { key = it },
            modifier = Modifier.fillMaxWidth(),
            trailingIcon = {
                Icon(imageVector = Icons.Default.Search, contentDescription = "", modifier = Modifier.clickable {
                    if (key.isNotEmpty()) {
                        //如果搜索记录不为空,添加数据源
                        list.add(key)
                        //清空搜索框
                        key = ""
                    }
                })
            })
        FlowRow(
            modifier = Modifier.padding(10.dp),
            horizontalArrangement = Arrangement.spacedBy(10.dp),
            verticalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            list.forEach {
                //如果一个方法的参数是另外一个方法,可以把大括号放在外面,但是必须得是最后一个参数是方法才行
                FlowItem(text = it) {
                    key = it
                }
            }
        }
    }
}

//() -> Unit 代表当前参数  () 代表当前参数是个方法,-> 代表当前方法的返回值 Unit 相当于Java中的void 代表当前方法没有返回值
@Composable
fun FlowItem(text: String, onClick: () -> Unit) {
    Text(
        text = text, modifier = Modifier
            .background(Color.Yellow, RoundedCornerShape(20.dp))
            .border(
                1.dp, Color.Gray,
                RoundedCornerShape(20.dp)
            )
            .padding(10.dp, 5.dp)
            .clickable {
                //当搜索记录被点击的时候
                //调用参数中的方法
                onClick()
            },
        maxLines = 1, overflow = TextOverflow.Ellipsis
    )
}